﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffPDA
{
    /*
     *  Class: EndMatch
     *  
     *  Use: Form to display options at end of a match  
     *  
     *  Author: Mike Skaife, mcs3@aber.ac.uk
     */
    public partial class EndMatch : Form
    {
        //Details for connecting to database
        string conString;
        SqlCeConnection conn;

        //The current Match
        Match theMatch;
        
        public EndMatch(Match match)
        {
            InitializeComponent();
            theMatch = match;
        }
        
        //This code runs when the 'End Match' button is clicked
        private void endEndBtn_Click(object sender, EventArgs e)
        {
            //Confirm user wants to end the match
            if ((MessageBox.Show("Are you sure you want to end the match?", "End Match", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) == DialogResult.Yes)
            {                
                //Close this form, and the main Match screen
                this.Visible = false;
                theMatch.Visible = false;

                //Close the port connecting the GPS antenna
                theMatch.gpsReader.closePort();
            }
        }

        //This code runs when the 'Extra Time' button is clicked
        private void endExtraBtn_Click(object sender, EventArgs e)
        {
            //Close this form and open the 'ExtraTime' form
            theMatch.extraTime.Visible = true;
            this.Visible = false;
        }

        //This code runs when the 'Penalty Shootout' button is clicked
        private void endPenBtn_Click(object sender, EventArgs e)
        {
            //Confirm user wants to start a penalty shootout
            if ((MessageBox.Show("Are you sure you want to start a penalty shootout?", "Penalty Shootout", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) == DialogResult.Yes)
            {
                //Set the timer to read '90:00'
                theMatch.tenMinutes = 9;
                theMatch.minutes = 0;
                theMatch.tenSeconds = 0;
                theMatch.seconds = 0;
                theMatch.MatchTimerLLbl.Text = "" + theMatch.tenMinutes + theMatch.minutes + ":" + theMatch.tenSeconds + theMatch.seconds;
                
                //Show the main Match screen again and close this form
                theMatch.Visible = true;
                this.Visible = false;
            }
        }

        //This code runs when the 'Restart Match' button is clicked
        private void endRestartBtn_Click(object sender, EventArgs e)
        {
            //Confirm user wants to restart the match
            if ((MessageBox.Show("Are you sure you want to restart the match?", "Restart Match", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) == DialogResult.Yes)
            {
                //Close the main Match form
                theMatch.Visible = false;

                //Reset all data in the database for the current match
                connectToDatabase();
                string delete_cautions = @"DELETE FROM caution;";
                string delete_sendoffs = @"DELETE FROM sendoff;";
                string delete_goals = @"DELETE FROM goal;";
                string delete_subs = @"DELETE FROM sub;";
                string onpitch_players = @"UPDATE player SET on_pitch=1 WHERE reset_value=1;";
                string offpitch_players = @"UPDATE player SET on_pitch=0 WHERE reset_value=0;";
                using (SqlCeCommand reset_cautions = new SqlCeCommand(delete_cautions, conn))
                {
                    reset_cautions.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_sendoffs = new SqlCeCommand(delete_sendoffs, conn))
                {
                    reset_sendoffs.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_goals = new SqlCeCommand(delete_goals, conn))
                {
                    reset_goals.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_subs = new SqlCeCommand(delete_subs, conn))
                {
                    reset_subs.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_onpitch = new SqlCeCommand(onpitch_players, conn))
                {
                    reset_onpitch.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_offpitch = new SqlCeCommand(offpitch_players, conn))
                {
                    reset_offpitch.ExecuteNonQuery();
                }
                closeDatabaseConnection();

                //Get hold of club data to be displayed on main Match screen
                string command = @"SELECT name, club_id FROM club";
                SqlCeDataAdapter da = new SqlCeDataAdapter(command, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "club_names");
                DataTable dt = ds.Tables["club_names"];

                String home_club_name = (string)ds.Tables[0].Rows[0]["name"];
                String away_club_name = (string)ds.Tables[0].Rows[1]["name"];
                int home_club_id = (int)ds.Tables[0].Rows[0]["club_id"];
                int away_club_id = (int)ds.Tables[0].Rows[1]["club_id"];

                //Re-create Match object so match can be restarted
                theMatch = new Match(home_club_id, away_club_id);

                //Set text on main Match screen buttons
                theMatch.MatchHomeTeamBtn.Text = home_club_name;
                theMatch.MatchAwayTeamBtn.Text = away_club_name;

                //Display the main Match screen so the match can be restarted
                theMatch.Visible = true;
                closeDatabaseConnection();

                //Close this EndMatch form
                this.Visible = false;
            }
        }

        //This code is run when the 'Abandon Match' button is clicked
        private void endAbandonBtn_Click(object sender, EventArgs e)
        {
            //Confirm the user wants to abandon the match
            if ((MessageBox.Show("Are you sure you want to abandon the match?", "Abandon Match", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) == DialogResult.Yes)
            {
                //Reset all the data in the database to return the match to its initial state
                connectToDatabase();
                string delete_cautions = @"DELETE FROM caution;";
                string delete_sendoffs = @"DELETE FROM sendoff;";
                string delete_goals = @"DELETE FROM goal;";
                string delete_subs = @"DELETE FROM sub;";
                string onpitch_players = @"UPDATE player SET on_pitch=1 WHERE reset_value=1;";
                string offpitch_players = @"UPDATE player SET on_pitch=0 WHERE reset_value=0;";
                using (SqlCeCommand reset_cautions = new SqlCeCommand(delete_cautions, conn))
                {
                    reset_cautions.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_sendoffs = new SqlCeCommand(delete_sendoffs, conn))
                {
                    reset_sendoffs.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_goals = new SqlCeCommand(delete_goals, conn))
                {
                    reset_goals.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_subs = new SqlCeCommand(delete_subs, conn))
                {
                    reset_subs.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_onpitch = new SqlCeCommand(onpitch_players, conn))
                {
                    reset_onpitch.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_offpitch = new SqlCeCommand(offpitch_players, conn))
                {
                    reset_offpitch.ExecuteNonQuery();
                }
                closeDatabaseConnection();

                //Close this form and the main Match form
                this.Visible = false;
                theMatch.Visible = false;

                //Close port connecting to GPS antenna
                theMatch.gpsReader.closePort();
            }
        }

        //This code runs when the 'Cancel' button is clicked
        private void cancelBtn_Click(object sender, EventArgs e)
        {
            //Restart timer, etc.
            theMatch.returnFromCancelEndMatch();

            //Close this form and display the main Match form again
            theMatch.Visible = true;
            this.Visible = false;
        }

        //Code to connect to the database
        private void connectToDatabase()
        {
            //Set the connection string so the database can be found
            conString = @"Data Source=\Program Files\RefOffPDA\RefOffPDA_DB.sdf";

            //Open the connection using the connection string
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //Code to disconnect from the database
        private void closeDatabaseConnection()
        {
            //Close the database connection
            conn.Close();
        }   
    }
}